package com.iplant.basic.serviceImpl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.basic.service.CRMService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.crm.*;
import com.iplant.basic.serviceImpl.dao.crm.*;

@Service
public class CRMServiceImpl implements CRMService {
    private static Logger logger = LoggerFactory.getLogger(CRMServiceImpl.class);

    private static CRMService Instance = null;

    public static CRMService getInstance() {
        if (Instance == null)
            Instance = new CRMServiceImpl();

        return Instance;
    }

    @Override
    public ServiceResult<Integer> CRM_UpdateCustomer(BMSEmployee wLoginUser, CRMCustomer wCustomer) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            CRMCustomerDAO.getInstance().UpdatePo(wLoginUser, wCustomer, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> CRM_ActiveCustomer(BMSEmployee wLoginUser, CRMCustomer wCustomer, int wActive) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            CRMCustomerDAO.getInstance().ActivePO(wLoginUser, wCustomer, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<CRMCustomer>> CRM_QueryCustomerList(BMSEmployee wLoginUser, String wTaxCode, String wName,
                                                                  String wIndustry, String wCountry, String wProvince, String wCity, int wGrade, int wType, int wActive,
                                                                  Pagination wPagination) {
        // TODO Auto-generated method stub
        ServiceResult<List<CRMCustomer>> wResult = new ServiceResult<List<CRMCustomer>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = CRMCustomerDAO.getInstance().CRM_QueryCustomerList(wLoginUser, wTaxCode, wName, wIndustry,
                    wCountry, wProvince, wCity, wGrade, wType, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<CRMCustomer> CRM_QueryCustomer(BMSEmployee wLoginUser, int wCustomerID, String wCode) {
        // TODO Auto-generated method stub
        ServiceResult<CRMCustomer> wResult = new ServiceResult<CRMCustomer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = CRMCustomerDAO.getInstance().SelectPO(wLoginUser, wCustomerID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> CRM_DeleteCustomer(BMSEmployee wLoginUser, CRMCustomer wCustomer) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            CRMCustomerDAO.getInstance().DeletePO(wLoginUser, wCustomer, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<Integer> CRM_UpdateCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            CRMCustomerTypeDAO.getInstance().CRM_UpdateCustomerType(wLoginUser, wCustomerType, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> CRM_ActiveCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType, int wActive) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            CRMCustomerTypeDAO.getInstance().CRM_ActiveCustomerType(wLoginUser, wCustomerType, wActive, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<CRMCustomerType>> CRM_QueryCustomerTypeList(BMSEmployee wLoginUser, String wName,
                                                                          int wActive,
                                                                          Pagination wPagination) {
        // TODO Auto-generated method stub
        ServiceResult<List<CRMCustomerType>> wResult = new ServiceResult<List<CRMCustomerType>>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = CRMCustomerTypeDAO.getInstance().CRM_QueryCustomerTypeList(wLoginUser, wName, wActive, wPagination, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<CRMCustomerType> CRM_QueryCustomerType(BMSEmployee wLoginUser, int wCustomerTypeID, String wCode) {
        // TODO Auto-generated method stub
        ServiceResult<CRMCustomerType> wResult = new ServiceResult<CRMCustomerType>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            wErrorCode.set(0);
            wResult.Result = CRMCustomerTypeDAO.getInstance().CRM_QueryCustomerType(wLoginUser, wCustomerTypeID, wCode, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> CRM_DeleteCustomerType(BMSEmployee wLoginUser, CRMCustomerType wCustomerType) {
        // TODO Auto-generated method stub
        ServiceResult<Integer> wResult = new ServiceResult<Integer>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>();
            CRMCustomerTypeDAO.getInstance().CRM_DeleteCustomerType(wLoginUser, wCustomerType, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            logger.error(e.toString());
        }
        return wResult;
    }


}
